<?php
// 验证转换后的路由地址是否在新项目中存在

// 定义已知的路由映射，基于我们检查到的前端项目结构
$knownRoutes = [
    // 系统管理 (/system)
    '/system/config' => 'views/home/ConfigList.vue',
    '/system/modules' => 'views/home/ModuleList.vue',
    '/system/module-config' => 'views/home/DataAuthList.vue',
    '/system/nodes' => 'views/home/RuleList.vue',
    '/system/roles' => 'views/home/RoleList.vue',
    '/system/logs' => 'views/home/LogList.vue',
    '/system/files' => 'views/home/FilesList.vue',
    '/system/database/backup' => 'views/home/DatabaseList.vue',
    '/system/database/restore' => 'views/home/DatabaseBackupList.vue',
    '/system/tasks' => 'views/home/TaskList.vue',
    '/system/templates' => 'views/home/TemplateList.vue',
    '/system/enterprise' => 'views/home/CateList.vue',
    '/system/regions' => 'views/home/AreaList.vue',
    
    // OA办公 (/oa)
    '/oa/plans' => 'views/oa/Plan.vue',
    '/oa/plans/calendar' => 'views/oa/Plan.vue', // 同一个组件，不同视图
    '/oa/schedules' => 'views/oa/Schedule.vue',
    '/oa/schedules/calendar' => 'views/oa/Schedule.vue', // 同一个组件，不同视图
    '/oa/works' => 'views/oa/Work.vue',
    '/oa/announcements' => null, // 未找到对应视图
    '/oa/news' => null, // 未找到对应视图
    '/oa/meetings' => null, // 未找到对应视图
    
    // 合同管理 (/contract)
    '/contract/categories' => 'views/contract/ContractCateList.vue',
    '/contract/product-categories' => 'views/contract/ProductCateList.vue',
    '/contract/products' => 'views/contract/ProductList.vue',
    '/contract/services' => 'views/contract/ServiceList.vue',
    '/contract/suppliers' => 'views/contract/SupplierList.vue',
    '/contract/purchased-categories' => 'views/contract/PurchasedCateList.vue',
    '/contract/purchased' => 'views/contract/PurchasedList.vue',
    '/contract/sales' => 'views/contract/SaleContractList.vue',
    '/contract/archived' => 'views/contract/SaleContractArchiveList.vue',
    '/contract/stopped' => 'views/contract/SaleContractStopList.vue',
    '/contract/voided' => 'views/contract/SaleContractVoidList.vue',
    
    // 客户管理
    '/customers' => 'views/customer/CustomerList.vue',
    '/customers/trash' => 'views/customer/CustomerList.vue', // 同一个组件，不同状态
    '/customer/grades' => null, // 未找到对应视图
    '/customer/sources' => null, // 未找到对应视图
    '/customer/industries' => null, // 未找到对应视图
    '/customer/contacts' => 'views/customer/CustomerContact.vue',
    '/customer/opportunities' => 'views/customer/CustomerChance.vue',
    '/customer/traces' => 'views/customer/CustomerTrace.vue',
    
    // 项目管理
    '/projects' => 'views/project/ProjectList.vue',
    '/projects/steps' => null, // 未找到对应视图
    '/projects/categories' => null, // 未找到对应视图
    '/projects/works' => null, // 未找到对应视图
    '/projects/documents' => null, // 未找到对应视图
    '/tasks' => null, // 未找到对应视图
    '/tasks/hours' => null, // 未找到对应视图
    '/tasks/comments' => null, // 未找到对应视图
    
    // 财务管理
    '/finance/expense-categories' => null, // 未找到对应视图
    '/finance/cost-categories' => null, // 未找到对应视图
    '/finance/expenses' => 'views/finance/Expense.vue',
    '/finance/expense-records' => 'views/finance/Expense.vue', // 同一个组件
    '/finance/invoices' => 'views/finance/Invoice.vue',
    '/finance/invoice-records' => 'views/finance/Invoice.vue', // 同一个组件
    '/finance/tickets' => 'views/finance/Ticket.vue',
    '/finance/ticket-records' => 'views/finance/Ticket.vue', // 同一个组件
    '/finance/incomes' => null, // 未找到对应视图
    '/finance/income-records' => null, // 未找到对应视图
    '/finance/payments' => null, // 未找到对应视图
    '/finance/payment-records' => null, // 未找到对应视图
    
    // 人事管理 (/hr)
    '/hr/reward-categories' => null, // 未找到对应视图
    '/hr/care-categories' => null, // 未找到对应视图
    '/hr/basic-data' => null, // 未找到对应视图
    '/hr/departments' => 'views/user/Department.vue',
    '/hr/positions' => 'views/user/Position.vue',
    '/hr/employees' => 'views/user/User.vue',
    '/hr/archives' => null, // 未找到对应视图
    '/hr/transfers' => null, // 未找到对应视图
    '/hr/leaves' => null, // 未找到对应视图
    '/hr/rewards' => null, // 未找到对应视图
    '/hr/cares' => null, // 未找到对应视图
    '/hr/contracts' => null, // 未找到对应视图
    
    // 行政管理 (/admin)
    '/admin/flow-modules' => null, // 未找到对应视图
    '/admin/flow-categories' => null, // 未找到对应视图
    '/admin/flows' => 'views/home/ApproveAllList.vue',
    '/admin/asset-categories' => null, // 未找到对应视图
    '/admin/asset-brands' => null, // 未找到对应视图
    '/admin/asset-units' => null, // 未找到对应视图
    '/admin/seal-categories' => null, // 未找到对应视图
    '/admin/basic-data' => null, // 未找到对应视图
    '/admin/assets' => null, // 未找到对应视图
    '/admin/asset-repairs' => null, // 未找到对应视图
    '/admin/cars' => null, // 未找到对应视图
    '/admin/car-repairs' => null, // 未找到对应视图
    '/admin/car-maintenances' => null, // 未找到对应视图
    '/admin/car-fees' => null, // 未找到对应视图
    '/admin/meeting-rooms' => null, // 未找到对应视图
    '/admin/meeting-records' => null, // 未找到对应视图
    '/admin/official-docs' => null, // 未找到对应视图
    '/admin/official-docs/pending' => 'views/home/ApproveCheckList.vue',
    '/admin/official-docs/reviewed' => 'views/home/ApproveCopyList.vue',
    '/admin/seal-applications' => null, // 未找到对应视图
    '/admin/seal-records' => null, // 未找到对应视图
    '/admin/note-categories' => null, // 未找到对应视图
    '/admin/notes' => null, // 未找到对应视图
    '/admin/news' => null, // 未找到对应视图
    
    // 网盘管理 (/disk)
    '/disk/personal' => 'views/disk/Disk.vue',
    '/disk/shared' => 'views/disk/Disk.vue', // 同一个组件，不同状态
    '/disk/trash' => 'views/disk/Disk.vue', // 同一个组件，不同状态
    
    // 案件管理 (/cases)
    '/cases' => 'views/lawcase/Lawcase.vue',
    '/cases/parties' => null, // 未找到对应视图
    '/cases/documents' => null, // 未找到对应视图
    '/cases/causes' => null, // 未找到对应视图
    '/cases/courts' => null, // 未找到对应视图
    '/cases/progress' => null, // 未找到对应视图
    
    // 律师管理 (/lawyers)
    '/lawyers/firms' => 'views/lawyer/Lawfirm.vue',
    '/lawyers' => 'views/lawyer/Lawyer.vue',
];

// 数据库连接配置
$host = '127.0.0.1';
$dbname = 'oa5';
$username = 'root';
$password = '123456';

try {
    $pdo = new PDO("mysql:host=$host;dbname=$dbname;charset=utf8", $username, $password);
    $pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
    
    // 查询所有已更新的菜单项
    $sql = "SELECT id, title, src, mig_src 
            FROM oa_admin_rule 
            WHERE src != '' AND menu = 1 AND status = 1 AND mig_src != '' 
            ORDER BY module, id";
    
    $stmt = $pdo->prepare($sql);
    $stmt->execute();
    $menuItems = $stmt->fetchAll(PDO::FETCH_ASSOC);
    
    echo "路由验证结果:\n";
    echo str_repeat("=", 100) . "\n";
    printf("%-5s %-15s %-30s %-30s %-10s\n", "ID", "标题", "原路径", "新路径", "状态");
    echo str_repeat("-", 100) . "\n";
    
    $validCount = 0;
    $invalidCount = 0;
    $missingViews = [];
    
    foreach ($menuItems as $item) {
        $migSrc = $item['mig_src'];
        $status = '未知';
        
        if (isset($knownRoutes[$migSrc])) {
            if ($knownRoutes[$migSrc] !== null) {
                $status = '✓ 存在';
                $validCount++;
            } else {
                $status = '✗ 视图缺失';
                $invalidCount++;
                $missingViews[] = [
                    'id' => $item['id'],
                    'title' => $item['title'],
                    'path' => $migSrc
                ];
            }
        } else {
            $status = '? 未验证';
        }
        
        printf("%-5s %-15s %-30s %-30s %-10s\n", 
               $item['id'],
               mb_substr($item['title'], 0, 15),
               mb_substr($item['src'], 0, 30),
               mb_substr($migSrc, 0, 30),
               $status);
    }
    
    echo str_repeat("=", 100) . "\n";
    echo "验证统计:\n";
    echo "  总数: " . count($menuItems) . " 个菜单项\n";
    echo "  有效路由: {$validCount} 个\n";
    echo "  视图缺失: {$invalidCount} 个\n";
    echo "  未验证: " . (count($menuItems) - $validCount - $invalidCount) . " 个\n";
    
    if (!empty($missingViews)) {
        echo "\n视图缺失的路由:\n";
        echo str_repeat("-", 60) . "\n";
        foreach ($missingViews as $missing) {
            echo "  ID: {$missing['id']}, 标题: {$missing['title']}, 路由: {$missing['path']}\n";
        }
    }
    
    echo "\n说明:\n";
    echo "  ✓ 存在: 路由和对应视图文件都存在\n";
    echo "  ✗ 视图缺失: 路由已定义但对应的视图文件不存在\n";
    echo "  ? 未验证: 路由未在验证列表中\n";
    echo "  注意: 同一个组件可能用于多个路由，所以视图文件数量少于路由数量是正常的\n";
    
} catch(PDOException $e) {
    echo "错误: " . $e->getMessage() . "\n";
}